    <!DOCTYPE html>
<html lang="zh-CN">
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta name="author" content="nodejh">
		<meta name="description" content="Describe your website">
		<meta name="generator" content="Hugo 0.16" />
		<title>Build MXNet on macOS &middot; nodejh</title>
		<link rel="shortcut icon" href="http://nodejh.com/images/favicon.ico">
		<link rel="stylesheet" href="http://nodejh.com/css/style.css">
		
		<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/styles/atom-one-light.min.css">
		

		
		<link rel="stylesheet" href="http://nodejh.com/fontawesome/css/font-awesome.min.css">
		

		
		<link href="http://nodejh.com/index.xml" rel="alternate" type="application/rss+xml" title="nodejh" />
		
	</head>

    <body>
       <nav class="main-nav">
	
	
		<a href='http://nodejh.com/'> <span class="arrow">←</span>Home</a>
	

	
		<a href='http://nodejh.com/post'>Archive</a>
	

	
		<a href='http://nodejh.com/about'>About</a>
	

	
	<a class="cta" href="http://nodejh.com/index.xml">Subscribe</a>
	
</nav>


        <section id="wrapper">
            <article class="post">
                <header>
                    <h1>Build MXNet on macOS</h1>
                    <h2 class="headline">
                    Oct 27, 2016 15:37
                    · 298 words
                    · 2 minutes read
                      <span class="tags">
                      
                      
                          
                              <a href="http://nodejh.com/tags/mxnet">MXNet</a>
                          
                              <a href="http://nodejh.com/tags/deep-learning">Deep Learning</a>
                          
                      
                      
                      </span>
                    </h2>
                </header>
                <section id="post-body">
                    

<p>MXNet 是一个深度学习系统。关于 MXNet 的介绍可以看这篇文章：<a href="https://github.com/dmlc/mxnet/issues/797">《MXNet设计和实现简介》</a>。</p>

<p>在 macOS 上编译安装 MXNet 的大体步骤都是按照官方文档来进行安装即可。但由于每个人电脑环境不同，所以可能会出现一些依赖库／包的缺失，导致安装失败。</p>

<h4 id="安装依赖软件">安装依赖软件</h4>

<p>在 macOS 上，首先需要具有以下软件：</p>

<ul>
<li>Homebrew (to install dependencies)</li>
<li>Git (to pull code from GitHub)</li>
<li>Homebrew/science (for linear algebraic operations)</li>
<li>OpenCV (for computer vision operations)</li>
</ul>

<p>如果上述已经安装了，就不需要再安装；如果没有，则按照下面的步骤安装：</p>

<pre><code># 安装 Homebrew
$ /usr/bin/ruby -e &quot;$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)&quot;
# 安装 Git 和 OpenCV
$ brew update
$ brew install git
$ brew tap homebrew/science
$ brew info opencv
$ brew install opencv
</code></pre>

<h4 id="编译-mxnet">编译 MXNet</h4>

<pre><code># 下载源码
$ git clone --recursive https://github.com/dmlc/mxnet
</code></pre>

<p>然后还需要安装 <code>openblas</code>：</p>

<pre><code># 安装 openblas
$ brew install --fresh -vd openblas
...
Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openblas/lib
    CPPFLAGS: -I/usr/local/opt/openblas/include

==&gt; Summary
🍺  /usr/local/Cellar/openblas/0.2.18_2: 20 files, 41.8M, built in 12 minutes 33 seconds
</code></pre>

<p>如果没有安装 <code>openblas</code>，则会有类似 <code>fatal error: 'cblas.h' file not found</code> 的错误，详见 <a href="https://github.com/dmlc/mxnet/issues/572">https://github.com/dmlc/mxnet/issues/572</a>。</p>

<p>接下来修改配置文件：</p>

<pre><code>$ cd mxnet
$ cp make/osx.mk ./config.mk
</code></pre>

<p>用 vim 或其他编辑器打开 <code>config.mk</code>，在 <code>USE_BLAS = apple</code> 下面加入如下 <code>ADD_LDFLAGS = -I/usr/local/opt/openblas/lib</code> 和 <code>ADD_CFLAGS =  -I/usr/local/opt/openblas/include</code>：</p>

<pre><code>USE_BLAS = apple
ADD_LDFLAGS = -I/usr/local/opt/openblas/lib
ADD_CFLAGS =  -I/usr/local/opt/openblas/include
</code></pre>

<p>最后再编译即可：</p>

<pre><code>$ make -j$(sysctl -n hw.ncpu)
</code></pre>

<h4 id="在-python-中使用-mxnet">在 Python 中使用 MXNet</h4>

<p>编译安装完成之后，若要使用 MXNet 的 Python 接口，还需要将 mxnet/python 添加到 Python 的包搜索路径。至少有三种方式可以实现。</p>

<p><strong>1. python 代码手动加载</strong></p>

<pre><code>import os, sys;
cur_path = os.path.abspath(os.path.dirname(__file__));
mxnet_lib_path = os.path.join(cur_path, 'mxnet/python');
sys.path.append(mxnet_lib_path);
import mxnet as mx;
</code></pre>

<p>在没有将 <code>mxnet/python</code> 添加到 PYTHONPATH 之前，依旧可以运行 <code>/example/image-classification</code> 里面的一些测试案例，就是因为案例里面有一行 <code>import find_mxnet</code>，而 <code>find_mxnet</code> 的作用就是手动加载 <code>mxnet/python</code>：</p>

<pre><code># find_mxnet.py
try:
    import mxnet as mx
except ImportError:
    import os, sys
    curr_path = os.path.abspath(os.path.dirname(__file__))
    sys.path.append(os.path.join(curr_path, &quot;../../python&quot;))
    import mxnet as mx

</code></pre>

<p><strong>2. 将路径加到环境变量 PYTHONPATH 中</strong></p>

<p>这种方法需要修改 shell 的配置文件。如果使用的 bash，则修改 <code>~/.bashrc</code>；若使用的是 zsh，则修改 <code>~/.zshrc</code>；其他类似。</p>

<p>在 bash 配置文件中加入下面这一行：</p>

<pre><code>export PYTHONPATH=path_to_mxnet_root/python
</code></pre>

<p>其中 <code>path_to_mxnet_root</code> 是下载的 mxnet 源码目录。</p>

<p><strong>3. 全局安装 mxnet</strong></p>

<p>直接运行 <code>mxnet/python/setup.py</code>，将 mxnet 添加到全局路径即可：</p>

<pre><code>python setup.py install --user
</code></pre>

<p>运行上面的命令后，脚本会在 <code>~/.local</code> 目录下创建一个 <code>lib</code> 目录，里面有一个 <code>python-2.7/site-packages</code> 文件夹。</p>

<p>如果是 <code>sudo python setup.py install</code>，则上面的目录会在 <code>/usr/lib</code> 下。</p>

<hr />

<ul>
<li><a href="http://mxnet.io/get_started/setup.html#build-mxnet-on-os-x-mac">Build MXNet on OS X (Mac)</a></li>
<li><a href="http://www.cnblogs.com/dengdan890730/p/5587542.html">mxnet的python包导入的前前后后</a></li>
</ul>

                </section>
            </article>

            <div id="disqus_thread"></div>
<div style="margin-top: 50px">
  评论: 请点击 Gtihub Issue 链接跳转到评论页面
</div>
</div>


            <footer id="footer">
    
        <div id="social">

	
	
    <a class="symbol" href="https://www.facebook.com/nodejh">
        <i class="fa fa-facebook-square"></i>
    </a>
    
    <a class="symbol" href="https://www.github.com/nodejh">
        <i class="fa fa-github"></i>
    </a>
    
    <a class="symbol" href="http://stackoverflow.com/users/4518882/nodejh">
        <i class="fa fa-stack-overflow"></i>
    </a>
    
    <a class="symbol" href="https://www.twitter.com/nodejh">
        <i class="fa fa-twitter"></i>
    </a>
    


</div>

    
    <p class="small">
    
        © Copyright 2016 <i class="fa fa-heart" aria-hidden="true"></i> nodejh
    
    </p>
</footer>

        </section>

        <script   src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI="   crossorigin="anonymous"></script>
<script src="http://nodejh.com/js/main.js"></script>

<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.7.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>




<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-84989670-1', 'auto');
ga('send', 'pageview');
</script>


    </body>
</html>
